package merlin.servlets;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class SearchFilter implements Filter{

	public void init(FilterConfig filterConfig)throws ServletException{
	}

	public void doFilter(ServletRequest req,
						ServletResponse res,
						FilterChain chain)throws IOException, ServletException {

		if(req instanceof HttpServletRequest) {
      		HttpServletRequest request = (HttpServletRequest) req;
			HttpServletResponse response = (HttpServletResponse) res;
			String requestUrl = request.getRequestURL().toString();
			System.out.println("reached the filter URL : " + requestUrl);

			if(requestUrl.endsWith("searchContent.jsp") && request.getMethod().equals("POST")){
				// forward to mr_searchReports.jsp
				System.out.println("forwarding");
				String searchtxt = request.getParameter("edtSearch");
				String url = "/merlin?pageName=mr_searchReports.jsp&mode=searchReportsByReportDesc&txtSearch=" + searchtxt;
				//RequestDispatcher dispatcher = request.getRequestDispatcher("mr_searchReports.jsp");
				RequestDispatcher dispatcher = request.getRequestDispatcher(url);
				dispatcher.forward(request, response);
			}else{
				System.out.println("in else");
				chain.doFilter(req, res);
			}
		}
	}

	public void destroy(){
	}
}